#include "xbutton.h"

button_t btn1;
button_t btn2;

uint8_t read_button1_GPIO()
{
    return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
}

uint8_t read_button2_GPIO()
{
    return HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin);
}

int xbutton_demo(void)
{
	uint8_t btn1_event_val = 0;
	
    button_init(&btn1, read_button1_GPIO, 0);
    button_init(&btn2, read_button2_GPIO, 0);

    button_bind(&btn1, B_EVENT_DOWN, BTN1_PRESS_DOWN_Handler);
    button_bind(&btn1, B_EVENT_UP, BTN1_PRESS_UP_Handler);
    button_bind(&btn1, B_EVENT_REPEAT, BTN1_PRESS_REPEAT_Handler);
    button_bind(&btn1, B_EVENT_CLICK, BTN1_SINGLE_Click_Handler);
    button_bind(&btn1, B_EVENT_DOUBLE_CLICK, BTN1_DOUBLE_Click_Handler);
    button_bind(&btn1, B_EVENT_PRESS_START, BTN1_LONG_PRESS_START_Handler);
    button_bind(&btn1, B_EVENT_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);

    button_bind(&btn2, B_EVENT_DOWN, BTN2_PRESS_DOWN_Handler);
    button_bind(&btn2, B_EVENT_UP, BTN2_PRESS_UP_Handler);
    button_bind(&btn2, B_EVENT_REPEAT, BTN2_PRESS_REPEAT_Handler);
    button_bind(&btn2, B_EVENT_CLICK, BTN2_SINGLE_Click_Handler);
    button_bind(&btn2, B_EVENT_DOUBLE_CLICK, BTN2_DOUBLE_Click_Handler);
    button_bind(&btn2, B_EVENT_PRESS_START, BTN2_LONG_PRESS_START_Handler);
    button_bind(&btn2, B_EVENT_PRESS_HOLD, BTN2_LONG_PRESS_HOLD_Handler);

    button_start(&btn1);
    button_start(&btn2);

    // make the timer invoking the button_ticks() interval 5ms.
    // This function is implemented by yourself.
    __timer_start(button_ticks, 0, 5);

    while (1) {
        if (btn1_event_val != button_get_event(&btn1)) {
            btn1_event_val = button_get_event(&btn1);

            if (btn1_event_val == B_EVENT_DOWN) {
                // do something
            } else if (btn1_event_val == B_EVENT_UP) {
                // do something
            } else if (btn1_event_val == B_EVENT_PRESS_HOLD) {
                // do something
            }
        }
    }
}

void BTN1_PRESS_DOWN_Handler(void* btn)
{
    // do something...
    printf("[%s] %d: \r\n", __func__, __LINE__);
}

void BTN1_PRESS_UP_Handler(void* btn)
{
    // do something...
    printf("[%s] %d: \r\n", __func__, __LINE__);
}

void BTN2_PRESS_DOWN_Handler(void* btn)
{
    // do something...
    printf("[%s] %d: \r\n", __func__, __LINE__);
}

void BTN2_PRESS_UP_Handler(void* btn)
{
    // do something...
    printf("[%s] %d: \r\n", __func__, __LINE__);
}

...